package com.atguigu.web;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Cart;
import com.atguigu.pojo.CartItem;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;
import com.google.gson.Gson;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

public class CartServlet extends BaseServlet {
    BookService bookService=new BookServiceImpl();
    /**
     * 加入购物车
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void addItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int bookId= WebUtils.parseInt(request.getParameter("id"),0);
        Book book=bookService.queryBookById(bookId);
        if(book!=null){
            CartItem cartItem=new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
            Cart cart=(Cart) request.getSession().getAttribute("cart");
            if(cart == null){
                cart=new Cart();
            }
            cart.addItem(cartItem);
            request.getSession().setAttribute("cart",cart);
            request.getSession().setAttribute("goodsName",request.getParameter("goodsName"));
            response.sendRedirect(request.getHeader("Referer"));//跳回原来请求发起的浏览器地址栏中的地址
        }
    }

    /**
     * 加入购物车
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void ajaxAddItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int bookId= WebUtils.parseInt(request.getParameter("id"),0);
        Book book=bookService.queryBookById(bookId);
        if(book!=null){
            CartItem cartItem=new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
            Cart cart=(Cart) request.getSession().getAttribute("cart");
            if(cart == null){
                cart=new Cart();
            }
            cart.addItem(cartItem);
            request.getSession().setAttribute("cart",cart);
            request.getSession().setAttribute("goodsName",book.getName());
            HashMap<String,Object> hashMap=new HashMap<>();
            hashMap.put("goodsName",book.getName());
            hashMap.put("count",cart.getTotalCount());
            Gson gson=new Gson();
            String mapToJson=gson.toJson(hashMap);
            response.getWriter().write(mapToJson);
            //response.sendRedirect(request.getHeader("Referer"));//跳回原来请求发起的浏览器地址栏中的地址
        }
    }
    protected void deleteCartItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer cartItemId=WebUtils.parseInt(request.getParameter("cartItemId"),0);
        Cart cart=(Cart) request.getSession().getAttribute("cart");
        if (cart !=null){
            cart.deleteItem(cartItemId);
        }
        response.sendRedirect(request.getHeader("Referer"));//跳回原来请求发起的浏览器地址栏中的地址
    }
    protected void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cart cart=(Cart) request.getSession().getAttribute("cart");
        if (cart !=null){
            cart.clear();
        }
        response.sendRedirect(request.getHeader("Referer"));//跳回原来请求发起的浏览器地址栏中的地址
    }
    protected void updateCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer goodsId=WebUtils.parseInt(request.getParameter("goodsId"),0);
        Integer count=WebUtils.parseInt(request.getParameter("count"),0);
        Cart cart=(Cart)request.getSession().getAttribute("cart");
        if(cart!=null){
            cart.updateCount(goodsId,count);
        }
        response.sendRedirect(request.getHeader("Referer"));//跳回原来请求发起的浏览器地址栏中的地址
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
